#ifndef SIMPLE_GRAPH_PARSING_H_
#define SIMPLE_GRAPH_PARSING_H_

#include "Graph.h"

namespace masugo
{

template<typename GraphType>
class simple_graph_parsing
{
public:
	typedef GraphType FGraph;

	static bool check_file_start(const char* filename)
	{
		std::ifstream input(filename, std::ios::in);
		if(input.good())
		{
			std::string line;
			std::getline(input, line);
			if(line.find("g ")==0) { return true; }
		}
		return false;
	}

	static std::deque<FGraph> read_graphs(const char* filename)
	{
		std::ifstream input(filename, std::ios::in);
		std::deque<FGraph> graphs;
		while(input.good())
		{
			std::string line;
			std::getline(input, line);
			std::istringstream line_stream(line);
			std::string record_type;
			line_stream >> record_type;
			if(record_type=="g")
			{
				graphs.push_back(FGraph());
				std::string name;
				line_stream >> name;
				graphs.back().set_name(name);
			}
			else if(record_type=="v" && !graphs.empty())
			{
				typename FGraph::VertexKey id=0;
				typename FGraph::VertexValue label=0;
				line_stream >> id >> label;
				graphs.back().set_vertex_value(id, label);
			}
			else if(record_type=="e" && !graphs.empty())
			{
				typename FGraph::VertexKey id1=0;
				typename FGraph::VertexKey id2=0;
				typename FGraph::EdgeValue label=0;
				line_stream >> id1 >> id2 >> label;
				graphs.back().connect(id1, id2, label);
			}
		}
		return graphs;
	}
};

}

#endif /* SIMPLE_GRAPH_PARSING_H_ */
